package cn.northsea.controller;

import cn.northsea.pojo.*;


import cn.northsea.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                          Model model,
                          @RequestParam(required = false, defaultValue = "") String start,
                          @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = null;
        PageInfo<Order> orderPageInfo = null;
        orderList = orderService.findAll(start, end);
        orderPageInfo = new PageInfo<>(orderList);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", orderPageInfo);
        return "orderList";
    }

    @RequestMapping("/addOrder")
    public String addOrder(Product product, Integer productNum, HttpSession httpSession, String orderAddress, String orderUphone) {
        User user = (User) httpSession.getAttribute("user");
        Double money = product.getProductMoney() * productNum;
        orderService.addOrder(money, product.getProductId(), new Date(System.currentTimeMillis()), user.getUserId(), orderAddress, orderUphone);
        return "redirect:/order/findAll1.do";
    }

    @RequestMapping("/findAll1")
    public String findAll1(HttpSession httpSession, Model model,
                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") String start,
                           @RequestParam(required = false, defaultValue = "") String end
    ) {
        PageHelper.startPage(pageNum, pageSize);
        User user = (User) httpSession.getAttribute("user");
        PageInfo<Order> orderPageInfo = null;
        List<Order> orderList = null;
        orderList = orderService.findAll1(user.getUserId(), start, end);
        orderPageInfo = new PageInfo<>(orderList);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", orderPageInfo);
        return "userOrder";
    }

    @RequestMapping("/refund")
    public String refund(Integer orderId) {
        orderService.refund(orderId);
        return "redirect:/order/findAll1.do";
    }
}
